<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 方式一：while循环
        // 查找 str里是否有SM 字符 如果有 那么就替换 这个str 如果没有停止
        var str = 'asdasdSMasdasdasdSMsdasdasdSMsadasd';

        function getstr(str) {
            while (str.indexOf("SM") != '-1') {
                str = str.replace("SM", "**");
            }                                                                                                                                                                                                               

            console.log(str);
            // 过滤敏感词
            //    var res =  str.replace("SM","**");

            return str;
            //    return res;
        }
        console.log(getstr(str));
        // 方式二、正则
        var all = "abcdcdefgdcd"
        all = all.replace(/cd/g, "**")
        console.log(all);
        // 方法三、replaceAll
        var all2 = "abcdcdefgdcd"
        all2 = all2.replaceAll("cd", "**")
        console.log(all2);
    </script>
</body>

</html>